package com.eati.cm.repo.fixed.base;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.GenericTypeResolver;
import org.springframework.data.repository.CrudRepository;

import javax.persistence.Id;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author 蘇智昊
 * 固定數據倉庫。
 * @see com.eati.cm.model.fixed.base.FixedDataType
 */
public abstract class FixedDataRepo<T, ID> implements CrudRepository<T, ID> {
    public abstract List<T> getData();

    @Override
    public long count(){
        return getData().size();
    }

    public void delete(){
        throw new UnsupportedOperationException("Cannot do delete operation to FixedDataRepo!");
    }

    @Override
    public void deleteAll(){
        delete();
    }

    @Override
    public void deleteAll(Iterable<? extends T> iterable) {
        delete();
    }

    @Override
    public void deleteById(ID id) {
        delete();
    }


    @Override
    public void delete(T entity){
        delete();
    }

    public void save(){
        throw new UnsupportedOperationException("Cannot do save operation to FixedDataRepo!");
    }

    @Override
    public <S extends T> S save(S s) {
        save();
        return null;
    }

    @Override
    public <S extends T> Iterable<S> saveAll(Iterable<S> iterable) {
        save();
        return null;
    }

    @Override
    public Optional<T> findById(ID id) {
        List<T> res = findAllById(Arrays.asList(id));
        return res.isEmpty() ? Optional.empty() : Optional.of(res.get(0));
    }

    @Override
    public boolean existsById(ID id) {
        List<T> res = findAllById(Arrays.asList(id));
        return !res.isEmpty();
    }

    @Override
    public Iterable<T> findAll() {
        return getData();
    }

    @Override
    public List<T> findAllById(Iterable<ID> id) {
        throw new UnsupportedOperationException("Cannot do findAllById on FixedDataRepo!");
    }


}
